package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.POIUtils;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingCntroller {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    private Result upload(@RequestParam("excelFile") MultipartFile excelFile){

        try {
            List<String[]> list = POIUtils.readExcel(excelFile);

            List<OrderSetting> data= new ArrayList<OrderSetting>();
            for (String[] strings : list) {
                String orderDate = strings[0];
                String number = strings[1];
                OrderSetting orderSetting = new OrderSetting(new Date(orderDate),Integer.parseInt(number));
                data.add(orderSetting);
            }
            //通过dubbo原创调用服务实现数据库批量调用
            orderSettingService.add(data);

            //返回数据成功
            return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS,data);
        } catch (IOException e) {

            //文件解析失败
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }


    }


    //根据月份查询对应的设置数据
    @RequestMapping("/getOrderSettingByMonth")
    private Result getOrderSettingByMonth(String date){

       try{
           List<Map> list= orderSettingService.getOrderSettingByMonth(date);
           return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
       }catch (Exception e){
           return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
       }
    }

    //修改指定日期修改可预约人数
    @RequestMapping("/editNumberByDate")
    private Result editNumberByDate(@RequestBody OrderSetting orderSetting){

       try{
           orderSettingService.editNumberByDate(orderSetting);
           //预约设置成功
           return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
       }catch (Exception e){
           //预约设置失败
           return new Result(false,MessageConstant.ORDERSETTING_FAIL);
       }
    }

}
